package com.winit.deploy.task.tomcatTask;

import com.winit.core.task.Task;
import com.winit.deploy.bean.WinitTomcatInfo;
import com.winit.deploy.server.WinitTomcatServer;
import com.winit.core.server.base.CmdOutputHandler;
import com.winit.deploy.util.DeployFileUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 * Created by wangjian on 16/8/10.
 * 发布任务step3 备份
 */
public class DeployBakTask extends Task {

    static final Logger logger = LoggerFactory.getLogger(DeployBakTask.class);

    public WinitTomcatServer winitTomcatServer;

    public DeployBakTask(String taskId, Integer expireTime, WinitTomcatServer winitTomcatServer) {
        super(taskId, expireTime);
        this.winitTomcatServer = winitTomcatServer;
    }

    @Override
    public TaskStatus call() throws Exception {
        logger.info("com.winit.deploy.task.tomcatTask.DeployBakTask.call start taskId=" + this.taskId);
        final TaskStatus[] taskStatus = {TaskStatus.STATUS_SUCCESS};
        CmdOutputHandler handler = new CmdOutputHandler() {

            @Override
            public void onMessage(String cmd, int index, String message, Boolean isErrorMessage) {
                if (isErrorMessage) {
                    taskStatus[0] = TaskStatus.STATUS_ERROR;
                } else {
                    taskStatus[0] = TaskStatus.STATUS_SUCCESS;
                }
                logger.info("DeployBakTask:执行备份,isErrorMessage=" + isErrorMessage + ",message=" + message);
            }
        };
        this.winitTomcatServer.setCmdOutputHandler(handler);
        this.winitTomcatServer.doBakUp();
        WinitTomcatInfo info = this.winitTomcatServer.getWinitTomcatInfo();
        DeployFileUtil.saveBackPackagePath(info.getEnvName(), info.getAreaName(), this.taskId, info.getBackUpPath());
        logger.info("com.winit.deploy.task.tomcatTask.DeployBakTask.call end taskId=" + this.taskId);
        return taskStatus[0];
    }

    public WinitTomcatServer getWinitTomcatServer() {
        return this.winitTomcatServer;
    }
}
